package com.choudou5.solr.dataimport.db;

import com.choudou5.solr.util.StrUtil;
import com.choudou5.solr.util.jdbc.DBUtil;
import com.choudou5.solr.dataimport.config.DIManager;
import com.choudou5.solr.dataimport.config.DIQuery;
import com.choudou5.solr.dataimport.config.DIQueryParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class QueryService {

	private static final Logger logger = LoggerFactory.getLogger(QueryService.class);

	/**
	 * 获取 记录总数
	 * @param param
	 * @return
	 */
	public static long getTotalCount(DIQueryParam param) {
		DIQuery query = DIManager.getQuery(param.getModule(), param.getType());
		if(query == null){
			logger.error("getTotalCount fail, module:{}, type:{} for sql not exists;", param.getModule(), param.getType().toLower());
			return 0;
		}
		long total = 0;
		String sql = query.getQueryCountSql();
		try {
			total = DBUtil.findCountBySql(sql);
		} catch (SQLException e) {
			logger.error("getTotalCount exception", e);
		}
		return total;
	}


	/**
	 * 获取 记录Ids列表 (走索引)
	 * @param param
	 * @param limit
	 * @param offset
	 * @return
	 */
	public static List<Long> getIdsList(DIQueryParam param, long limit, int offset) {
		DIQuery query = DIManager.getQuery(param.getModule(), param.getType());
		if(query == null){
			logger.error("getIdsList fail, module:{}, type:{} for sql not exists;", param.getModule(), param.getType().toLower());
			return null;
		}
		List<Long> idList = new ArrayList<Long>(offset);
		String sql = query.getQueryIdSql()+" LIMIT "+limit+","+offset;
		try {
			idList = DBUtil.findOneColumnList(sql, Long.class);
		} catch (SQLException e) {
			logger.error("getIdsList exception", e);
		}
		return idList;
	}


	/**
	 * 获取 记录列表
	 * @param param
	 * @param limit
	 * @param offset
	 * @return
	 */
	public static List<Map> getRows(DIQueryParam param, long limit, int offset) {
		DIQuery query = DIManager.getQuery(param.getModule(), param.getType());
		if(query == null){
			logger.error("getRows fail, module:{}, type:{} for sql not exists;", param.getModule(), param.getType().toLower());
			return Collections.EMPTY_LIST;
		}
		String sql = query.getQuerySql();
		List<Map> result = null;
		sql = sql+" LIMIT "+limit+","+offset;
		try {
			result = DBUtil.findList(sql, Map.class);
		} catch (SQLException e) {
			logger.error("getRows exception", e);
		}
    	return result;
    }

	/**
	 * 获取 记录列表
	 * @param param
	 * @param beginId
	 * @param endId
	 * @return
	 */
	public static List<Map> getRowsByBetween(DIQueryParam param, long beginId, long endId) {
		DIQuery query = DIManager.getQuery(param.getModule(), param.getType());
		if(query == null){
			logger.error("getRows fail, module:{}, type:{} for sql not exists;", param.getModule(), param.getType().toLower());
			return Collections.EMPTY_LIST;
		}
		String sql = query.getQuerySql();
		List<Map> result = null;
		try {
			sql = sql+" WHERE ID BETWEEN "+beginId+","+endId;
			result = DBUtil.findList(sql, Map.class);
		} catch (SQLException e) {
			logger.error("getRows exception", e);
		}
		return result;
	}

	/**
	 * 获取 记录列表
	 * @param param
	 * @param idList
	 * @return
	 */
	public static List<Map> getRows(DIQueryParam param, List<Long> idList) {
		DIQuery query = DIManager.getQuery(param.getModule(), param.getType());
		if(query == null){
			logger.error("getRows fail, module:{}, type:{} for sql not exists;", param.getModule(), param.getType().toLower());
			return Collections.EMPTY_LIST;
		}
		String sql = query.getQuerySql();
		List<Map> result = null;
		try {
			sql = sql+" WHERE ID IN("+StrUtil.join(",", idList)+")";
			result = DBUtil.findList(sql, Map.class);
		} catch (SQLException e) {
			logger.error("getRows exception", e);
		}
		return result;
	}

}
